package com.company.zhishidian.stream;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import java.util.stream.LongStream;
import java.util.stream.Stream;

/**
 * jdk1.8
 * Stream  功能超级强大 函数式编程
 * 微服务 分布式 webService
 * 聚合操作 数据库任务
 * Stream 介绍
 * java对聚合操作的流式的编写方式
 * 1. 中间操作 inter        Filter sort
 * 2. 终极操作 terminal     sum
 * 3. 短路操作 short-cu     limit
 * 中间操作可以有很多，终极操作有很多，且中间操作不会直接执行，只会记录有多少中间操作
 * 会执行到终极操作在把之前计陆的所有中间操作都执行，但不会影响执行效率，与for循环是一样的时间，
 */
public class Test {
    public static void main(String[] args) {
        // [1-100] 1.计算他们的和
        int sum = 0;
        for (int i = 1; i <= 100; i++) {
            sum+=i;
        }
        System.out.println(sum);

        // 用Stream操作
        int sum2 = IntStream.rangeClosed(1,100).sum();
        System.out.println(sum2);

        // [1-100] 计算偶数的和
        int sum3 = 0;
        for (int i = 1; i <= 100; i++) {
            if(i%2==0){
                sum3+=i;
            }
        }
        System.out.println(sum3);

        // 用Stream操作
        int sum4 = IntStream.rangeClosed(1,100).filter( i->{ return i%2==0;}).sum();
        System.out.println(sum4);
    }
}
class Test2{
    /**
     * list 放 Student
     * 所有女生，按照年龄倒序排序，生成一个list，放学生的姓名
     */
    public static void main(String[] args) throws FileNotFoundException {
        List<Student> list = new ArrayList<>(List.of(
                new Student("aaa","f",19),
                new Student("bbb","m",20),
                new Student("ccc","m",21),
                new Student("ddd","f",22),
                new Student("eee","m",23),
                new Student("fff","f",24),
                new Student("ggg","f",25)
                ));
        List<String> list1 = new ArrayList<>();
        Collections.sort(list,(x,y)-> y.getAge()-x.getAge());
        for (Student s:list) {
            if(s.getSex().equals("f")){
                list1.add(s.getName());
            }
        }
        System.out.println(list1);

        // 使用Stream
        list.stream().filter((s)->{return s.getSex().equals("f");}).
                peek(System.out::println).
                sorted(Comparator.comparing(Student::getAge).reversed()).
                map(Student::getName).collect(Collectors.toList()).forEach(System.out::println);

        // 遍历文件
        BufferedReader br = new BufferedReader(new FileReader("src.txt"));
        br.lines().forEach(System.out::println);
        // 字符串转成Stream
        Stream.of("aaa","bbb","cc");

        // 写入一到100
        LongStream.of(1,2,3);
        LongStream.range(1,100);  // 不包含100
        LongStream.rangeClosed(1,100);  // 包含100

        // 年龄最大的
        Student oldest = list.stream().max(Comparator.comparing(Student::getAge)).get();
        System.out.println(oldest);

        // 男生多少个
        long mcount = list.stream().filter(s->{return s.getSex().equals("f");}).count();
        System.out.println(mcount);

        // 年龄第三大的学生
        Student third = list.stream().sorted(Comparator.comparing(Student::getAge).reversed()).skip(2).findFirst().get();
        System.out.println(third);

        // 男生list
        List<Student> mList = List.of(
                new Student("bbb","m",20),
                new Student("ccc","m",21),
                new Student("eee","m",23)
        );
        // 女生list
        List<Student> fList = List.of(
                new Student("aaa","f",19),
                new Student("ddd","f",22),
                new Student("fff","f",24),
                new Student("ggg","f",25)
        );
        List<List<Student>> allList = List.of(mList,fList);

        List<String> nameList2 = allList.stream().flatMap((childList)->childList.stream()).map(Student::getName).collect(Collectors.toList());
        System.out.println(nameList2);
    }
}

class Student{
    private String name;
    private String sex;
    private int age;

    public Student(String name, String sex, int age) {
        this.name = name;
        this.sex = sex;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", sex='" + sex + '\'' +
                ", age=" + age +
                '}';
    }
}
